import 'package:common_utils/common_utils.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/city_information.dart';
import 'package:yp/helper.dart';
import 'package:yp/pages/address_list/logic.dart';
import 'package:yp/services/api.dart';

import 'state.dart';

class AddressUpdateLogic extends GetxController {
  final state = AddressUpdateState();

  @override
  void onInit() {
    super.onInit();
    String addressTag = SpUtil.getString("addressTag", defValue: "");

    if (addressTag.isNotEmpty) {
      state.tagList[2] = addressTag;
    }
    var cityParams = {
      "access_token": SpUtil.getString('token'),
    };
    Api.cityInformation(cityParams).listen((event) {
      // print('城市信息  event = $event');
      if (event['code'] == 0) {
        AddressInformation addressInformation =
            AddressInformation.fromJson(event);
        Map<String, dynamic> mapData = addressInformation.toJson();
        state.addressList = mapData['children'];
      }
      update();
    });
  }

  void setSex(int sex) {
    state.sex = sex;
    update();
  }

  @override
  void onReady() {
    super.onReady();
    var params = {
      'usr_num_id': SpUtil.getInt('empeNumId'),
      'access_token': SpUtil.getString('token'),
      "adr_num_id": state.addressId,
      'access_token': SpUtil.getString('token'),
    };
    //获取选择商品列表数据
    Api.editAddressMsg(params).listen((event) {
      state.controller1.text = event["member_adr"]["cont_empe"];
      state.controller2.text = event["member_adr"]["contact_no"];
      state.controller3.text = event["member_adr"]["prv_name"] + " " + event["member_adr"]["city_name"] + " " + event["member_adr"]["city_area_name"] + "${event["member_adr"]["town_name"] == null ? "" : " ${event["member_adr"]["town_name"]}"}";
      state.controller4.text = event["member_adr"]["adr"];
      state.provinceId = event["member_adr"]["prv_num_id"].toString();
      state.cityId = event["member_adr"]["city_num_id"].toString();
      state.areaId = event["member_adr"]["city_area_num_id"].toString();
      state.townId = event["member_adr"]["town_num_id"].toString();
      if (event["member_adr"]["default_sign"] == 1) {
        state.isSelect = true;
      } else {
        state.isSelect = false;
      }
      String gasTag = SpUtil.getString("addressTag", defValue: "");

      if (gasTag.isNotEmpty) {
        state.tagList[2] = gasTag;
      }
      if (event["member_adr"]["address_tag"] == "家") {
        state.sex = 1;
      } else if (event["member_adr"]["address_tag"] == "公司") {
        state.sex = 2;
      } else if (event["member_adr"]["address_tag"] != null) {
        state.sex = 3;
      }
      update();
    });
  }

  void updateAdress() {
    var arg = Get.arguments;
    print(">>>>>>>>>>>${Get.arguments}");
    if (!RegexUtil.isMobileSimple(state.controller2.text)) {
      toast("手机号格式错误");
      return;
    }

    var params = {
      'access_token': SpUtil.getString('token'),
      'usr_num_id': SpUtil.getInt('empeNumId'),
      "adr_num_id": state.addressId,
      "prv_num_id": state.provinceId,
      "city_num_id": state.cityId,
      "city_area_num_id": state.areaId,
      "town_num_id": state.townId,
      "adr": state.controller4.text,
      "mobile": state.controller2.text,
      "receiver": state.controller1.text,
      "default_sign": state.isSelect ? 1 : 0,
      'address_tag': state.tagList[state.sex - 1],
    };

    print("编辑地址的params：$params");

    //获取选择商品列表数据
    Api.editAddressUpdate(params).listen((event) {
      if (event["code"].toString() == '0') {
        Get.back(result: true);
      }
    });
  }

  /// 选择地址
  // void selectCity(value) {
  //   state.provinceId = int.parse(value.provinceCode);
  //   state.cityId = int.parse(value.cityCode);
  //   state.areaId = int.parse(value.areaCode);
  //   state.townId = int.parse(value.townCode);
  //   state.controller3.text =
  //       value.province + ' ' + value.city + ' ' + value.area;
  //   update();
  // }
  void selectCity(int index, Map checkedItem, List backAddressItemList) {
    switch (index) {
      case 0:
        state.province = checkedItem;
        state.provinceList = backAddressItemList;

        state.city = {};
        state.cityList = [];
        state.district = {};
        state.cityAreaList = [];
        state.street = {};
        state.townList = [];

        break;
      case 1:
        state.city = checkedItem;
        state.cityList = backAddressItemList;

        state.district = {};
        state.cityAreaList = [];
        state.street = {};
        state.townList = [];

        break;
      case 2:
        state.district = checkedItem;
        state.cityAreaList = backAddressItemList;

        state.street = {};
        state.townList = [];

        break;
      case 3:
        state.street = checkedItem;
        state.townList = backAddressItemList;
        break;
    }

    if (index == 3) {
      state.provinceName = state.province["name"];
      state.cityName = state.city["name"];
      state.areaName = state.district["name"];
      state.townName = state.street["name"];
      state.provinceId = state.province["id"];
      state.cityId = state.city["id"];
      state.areaId = state.district["id"];
      state.townId = state.street["id"];

      state.controller3.text = state.provinceName +' '+ state.cityName+' '+ state.areaName + (state.townName == null ? "" : (" " + state.townName));
      update();
    }
  }

  delAddress() {
    var params = {
      "adr_num_id": state.addressId, //地址编号
      'access_token': SpUtil.getString('token'),
    };
    EasyLoading.show();
    Api.deleteAss(params).listen((event) {
      EasyLoading.dismiss();
      // var listLogic = Get.find<AddressListLogic>();
      // listLogic.loadData();
      if (event['code'] == 0) {
        Get.back(result: true);
      } else {
        toast(event['message']);
      }
    }, onError: (error) {
      EasyLoading.dismiss();
    });
  }
}
